package com.westvalley.common.kit;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.westvalley.common.kit.StringKit;


public class SqlsKit {
	private String strTableName;	//表名称
	private SqlOperateType operateType;
	private Map<String,Object> columValueMap;
	private Map<String,Object> whereValueMap;
	private StringBuffer sql = new StringBuffer();
	
	public static SqlsKit me(){
		return new SqlsKit();
	}
	
	private SqlsKit(){
		clear();
	}
	
	/**
	 * 清空对象
	 */
	public void clear(){
		strTableName = "";	//表名称
		operateType = SqlOperateType.NULL;
		columValueMap = new LinkedHashMap<String, Object>();
		whereValueMap = new LinkedHashMap<String, Object>();
		sql = new StringBuffer();
	}
	
	/**
	 * 设置表名
	 * @param strTableName
	 */
	public void setTableName(String strTableName){
		this.strTableName = strTableName;
	}
	
	/**
	 * 添加where条件
	 * @param strWhere
	 */
	public void setWhere(String strColumn,Object value){
		whereValueMap.put(strColumn, value);
	}
	
	/**
	 * 添加数据
	 * @param strColumn 列名
	 * @param strValue	值
	 */
	public void addData(String strColumn,Object value){
		columValueMap.put(strColumn, value);
	}
	
	/**
	 * 转换操作占位符
	 * @return
	 */
	public List<String> getPlaceholder(Collection<String> collection){
		List<String> list = new ArrayList<String>();
		for(int i=0;i<collection.size();i++){
			list.add("?");
		}
		return list;
	}
	
	/**
	 * 获取添加sql
	 * @return
	 */
	public String getInsertSql(){
		if(StrKit.notBlank(strTableName)){
			operateType = SqlOperateType.INSERT;
			this.sql.append(" INSERT INTO ");
			this.sql.append(this.strTableName);
			this.sql.append(" ( ");
			this.sql.append( StringKit.set2String(columValueMap.keySet(), ",", false) );
			this.sql.append(" ) ");
			this.sql.append(" VALUES ");
			this.sql.append(" ( ");
			this.sql.append( StringKit.list2String(getPlaceholder(columValueMap.keySet()), ",", false) );
			this.sql.append(" ) ");
			return sql.toString();
		}else{
			throw new RuntimeException("请设置操作表");
		}
	}
	
	/**
	 * 获取更新sql
	 * @return
	 */
	public String getUpdateSql(){
		if(StrKit.notBlank(strTableName)){
			operateType = SqlOperateType.UPDATE;
			this.sql.append(" UPDATE ");
			this.sql.append(this.strTableName);
			this.sql.append(" SET ");
			StringBuffer str = new StringBuffer();
			for(String column : columValueMap.keySet()){
				str.append(",");
				str.append(column);
				str.append(" = ");
				str.append("?");
			}
			if(str.length()>0)
				this.sql.append(str.subSequence(1, str.length()));
			this.sql.append(" WHERE 1 = 1");
			for(String column : whereValueMap.keySet()){
				this.sql.append(" AND ");
				this.sql.append(column);
				this.sql.append(" = ");
				this.sql.append("?");
			}
			return this.sql.toString();
		}else{
			throw new RuntimeException("请设置操作表");
		}
	}
	
	public String getDeleteSql(){
		if(StrKit.notBlank(strTableName)){
			operateType = SqlOperateType.DELETE;
			this.sql.append(" DELETE FROM ");
			this.sql.append(this.strTableName);
			this.sql.append(" WHERE 1 = 1");
			for(String column : whereValueMap.keySet()){
				this.sql.append(" AND ");
				this.sql.append(column);
				this.sql.append(" = ");
				this.sql.append("?");
			}
			return this.sql.toString();
		}else{
			throw new RuntimeException("请设置操作表");
		}
	}
	
	public List<Object> getSqlValues(){
		if(operateType!=null && !SqlOperateType.NULL.equals(operateType)){
			if(StrKit.notBlank(strTableName)){
				List<Object> list = new ArrayList<Object>();
				if( SqlOperateType.INSERT.equals(operateType) || SqlOperateType.UPDATE.equals(operateType) ){
					for(String column : columValueMap.keySet()){
						list.add(columValueMap.get(column));
					}
				}
				if( SqlOperateType.UPDATE.equals(operateType) || SqlOperateType.DELETE.equals(operateType) ){
					for(String column : whereValueMap.keySet()){
						list.add(whereValueMap.get(column));
					}
				}
				return list;
			}else{
				throw new RuntimeException("请设置操作表");
			}
		}else{
			throw new RuntimeException("请设置操作类型");
		}
	}
	
	public String getSql(){
		if(SqlOperateType.INSERT.equals(operateType)){
			return getInsertSql();
		}else if(SqlOperateType.UPDATE.equals(operateType)){
			return getUpdateSql();
		}else if(SqlOperateType.DELETE.equals(operateType)){
			return getDeleteSql();
		}else{
			throw new RuntimeException("请设置操作类型");
		}
	}
	
	public void setOperateType(SqlOperateType operateType) {
		this.operateType = operateType;
	}

	public enum SqlOperateType{
		NULL,INSERT,UPDATE,DELETE
	}
	
}
